<?php
class codex
{
	var $db;
	var $table;
	var $table_log;
	var $model_table;
	var $table_category;
	var $ishtml = 0;
	var $userid = 0;
	var $userid_sql = '';
	var $pages;
	var $number;
	var $url;
	var $html;
	var $s;
	var $is_update_related = 1;

    function __construct()
    {
		global $db, $log, $MODULE;
		$this->db = &$db;
		$this->table = DB_PRE.'codex';
		//$this->table_count = DB_PRE.'codex_count';
		//$this->table_position = DB_PRE.'codex_position';
		//$this->table_tag = DB_PRE.'codex_tag';
		$this->table_category = DB_PRE.'category';
		//$this->log = is_object($log) ? $log : load('log.class.php','log');
		//$this->url = load('url.class.php','url');
		$this->modelid = 54; // 在model表查找
    }

	function codex()
	{
		$this->__construct();
	}

	function set_catid($catid)
	{
		global $CATEGORY, $MODEL;
		if(!isset($CATEGORY[$catid])) return false;
		 
		//$modelid = $CATEGORY[$catid]['modelid'];
		//if(!isset($MODEL[$modelid])) return false;
		//$this->modelid = $modelid;
		$this->ishtml = $MODEL[$this->modelid]['ishtml'];
		$this->model_table = DB_PRE.'codex_'.$MODEL[$this->modelid]['tablename'];
		return true;
	}

	function set_userid($userid)
	{
		$this->userid = intval($userid);
		$this->userid_sql = " AND $this->table.`userid`=$this->userid ";
	}

	function get($codexid, $tablecount = 2)
	{
		$codexid = intval($codexid);
		$data = $this->db->get_one("SELECT * FROM `$this->table` WHERE `codexid`=$codexid $this->userid_sql");
		if($data)
		{
			// 如果涉及到2个以上的表
			/*if($tablecount >= 2)
			{
				$this->set_catid($data['catid']);
				if(!$this->model_table) return false;
				$data2 = $this->db->get_one("SELECT * FROM `$this->model_table` WHERE `codexid`=$codexid");
				if($tablecount == 2 && is_array($data) && is_array($data2)) $data = array_merge($data, $data2);
			}*/
			
			// 如果涉及到3个表
			if($tablecount == 3)
			{
				$data3 = $this->db->get_one("SELECT * FROM `$this->table` WHERE `codexid`=$codexid");
				if(is_array($data) && is_array($data2) && is_array($data3)) $data = array_merge($data, $data2, $data3);
			}
		}
		
		return $data;
	}

	function add($data, $cat_selected = 0, $isimport = 0)
	{
		global $_userid, $_username,$CATEGORY, $MODEL, $SETTING;
		
		// data里面一定要有栏目id
		/*if(!$data['catid']){
			showmessage("请选择栏目id");
		}
        if(!$this->set_catid($data['catid'])) return false;*/
		
		
		require_once 'model/codex_input.class.php';
        require_once 'model/codex_update.class.php';
		$codex_input = new codex_input($this->modelid);
		$inputinfo = $codex_input->get($data, $isimport);
		$systeminfo = $inputinfo['system'];
		$modelinfo = $inputinfo['model'];
		if(!$systeminfo['username']) $systeminfo['username'] = $_username;
		if(!$systeminfo['userid']) $systeminfo['userid'] = $_userid;

		if($data['inputtime'])
		{
			$systeminfo['inputtime'] = strtotime($data['inputtime']);
		}
		else
		{
			$systeminfo['inputtime'] = TIME;
		}
		if($data['updatetime'])
		{
			$systeminfo['updatetime'] = strtotime($data['updatetime']);
		}
		else
		{
			$systeminfo['updatetime'] = TIME;
		}
		if(isset($data['paginationtype']))
		{
			$modelinfo['paginationtype'] = $data['paginationtype'];
			$modelinfo['maxcharperpage'] = $data['maxcharperpage'];
		}
		
  		$this->db->insert($this->table, $systeminfo);
		$codexid = $this->db->insert_id();
		
        $modelinfo['codexid'] = $codexid;
		//$this->db->insert($this->model_table, $modelinfo);
		
		// 内容页地址
		if($data['islink']==1)
		{
			$url = $data['linkurl'];
		}
		else
		{
			$url = $this->url($codexid, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix']);
		}
		
        $this->db->query("UPDATE `$this->table` SET `url`='$url' WHERE `codexid`=$codexid");
        $this->db->query("UPDATE `$this->table_category` SET `items`=`items`+1 WHERE `catid`='".$data['catid']."'");
	

		if(is_array($cat_selected) && !empty($cat_selected))
		{
			$relink_array['title'] = $data['title'];
			$relink_array['url'] = $url;
			$relink_array['islink'] = 1;
			$relink_array['username'] = $_username;
			$relink_array['userid'] = $_userid;
			$relink_array['inputtime'] = $systeminfo['inputtime'];
			$relink_array['updatetime'] = $systeminfo['updatetime'];
			$relink_array['status'] = $data['status'];
			foreach($cat_selected AS $cid)
			{
				$relink_array['catid'] = $cid;
				$relink_modelid = $CATEGORY[$cid]['modelid'];
				$relink_model_table = DB_PRE.'codex_'.$MODEL[$relink_modelid]['tablename'];
				$this->db->insert($this->table, $relink_array);
				$relink_codexid = $this->db->insert_id();
				$relinkinfo['codexid'] = $relink_codexid;
				$this->db->insert($relink_model_table, $relinkinfo);
			}
		}
		$codex_update = new codex_update($this->modelid, $codexid);
		$codex_update->update($data);
		
		// 如果栏目设置是静态化，这里生成静态页 $leotyn
		//$C = cache_read('category_'.$inputinfo['system']['catid'].'.php', '', 1);
		
		
		/*if($SETTING['ishtml']){
			$html = load('html.class.php','html');
			$html->show($codexid);
		}*/
		
		//if(!$isimport) $this->log_write($codexid, '', '', $data['islink']);
		return $codexid;
	}

	function edit($codexid, $data)
	{
		global $MODEL,$old_catid,$SETTING;
        //if(!$this->set_catid($data['catid'])) return false;
		
		/*if($old_catid && $old_catid!=$data['catid'])
		{
			$html = load('html.class.php','html');
			$html->delete($codexid, $this->model_table);
		}*/
		
		require_once 'model/codex_input.class.php';
        require_once 'model/codex_update.class.php';
		if(!$MODEL[$this->modelid]['isrelated']) $this->is_update_related = 0;

		$codex_input = new codex_input($this->modelid);
		$inputinfo = $codex_input->get($data);
		$systeminfo = $inputinfo['system'];
		$modelinfo = $inputinfo['model'];
		
		if(isset($data['paginationtype']))
		{
			$modelinfo['paginationtype'] = $data['paginationtype'];
			$modelinfo['maxcharperpage'] = $data['maxcharperpage'];
		}
		if($data['inputtime'])
		{
			$systeminfo['inputtime'] = strtotime($data['inputtime']);
		}
		else
		{
			$systeminfo['inputtime'] = TIME;
		}
		$systeminfo['updatetime'] = TIME;
		
		
		// 内容详细地址设置
		if($data['islink']==1)
		{
			$systeminfo['url'] = $data['linkurl'];
		}
		else
		{
			/*$prefix = $data['prefix'] ? $data['prefix'] : ($SETTING['enable_urlencode'] ? hash_string($codexid) : '');*/
			
			$systeminfo['url'] = $this->url($codexid, 0, $data['catid'], $systeminfo['inputtime'], $data['prefix']);

		}	
		$this->db->update($this->table, $systeminfo, "`codexid`=$codexid $this->userid_sql");
		unset($systeminfo['status']);
		///if($modelinfo) $this->db->update($this->model_table, $modelinfo, "`codexid`=$codexid");
		$codex_update = new codex_update($this->modelid, $codexid);
		$codex_update->update($data);
		
		
		// 如果栏目设置是静态化，这里生成静态页 $leotyn
		//$html = load('html.class.php','html');
		//$html->show($codexid);
		
		//$this->html($codexid);

		//$this->log_write($codexid, '', '', $data['islink']);
		return true;
	}

	function listinfo($where = '', $order = '`listorder` DESC,`codexid` DESC', $page = 1, $pagesize = 50)
	{
		//if($where) $where = " WHERE $where $this->userid_sql";
		
		// 指定会员发布的文章列表 aming
		if($this->userid){
			$where = $where ? " WHERE $where AND $this->table.`userid`=$this->userid " : " WHERE $this->table.`userid`=$this->userid ";
		}
		else{
			$where = " WHERE $where ";
		}
		
		if($order) $order = " ORDER BY $order";
		$page = max(intval($page), 1);
        $offset = $pagesize*($page-1);
        $limit = " LIMIT $offset, $pagesize";
        $number = cache_count("SELECT count(*) AS `count` FROM `$this->table` $where");
		
        $this->pages = pages($number, $page, $pagesize);
		$array = array();
		$result = $this->db->query("SELECT * FROM `$this->table` $where $order $limit");
		while($r = $this->db->fetch_array($result))
		{
		    $r['inputtime'] = $r['inputtime'];
		    $r['updatetime'] = $r['updatetime'];
			$array[] = $r;
		}
		$this->number = $this->db->num_rows($result);
        $this->db->free_result($result);
		return $array;
	}

	function listorder($info)
	{
		if(!is_array($info)) return false;
		foreach($info as $id=>$listorder)
		{
			$id = intval($id);
			$listorder = intval($listorder);
			$this->db->query("UPDATE `$this->table` SET `listorder`=$listorder WHERE `codexid`=$id $this->userid_sql");
			$this->log_write($id, 'listorder');
		}
		return true;
	}

	function search($sql, $page = 1, $pagesize = 20, $setting = array())
	{
		$page = max(intval($page), 1);
		$offset = $number*($page-1);
		$sql_count = preg_replace("/^SELECT([^(]+)FROM(.+)(ORDER BY.+)$/i", "SELECT COUNT(*) AS `count` FROM\\2", $sql);
		$count = cache_count($sql_count);
		$this->pages = pages($count, $page, $number);
		$data = array();
		$result = $db->query("$sql LIMIT $offset, $number");
		while($r = $db->fetch_array($result))
		{
			$data[] = $r;
		}
		$db->free_result($result);
		return $data;
	}

	function output($data)
	{
		require_once 'model/codex_output.class.php';
		$output = new codex_output();
		return $output->get($data);
	}

	function log_write($codexid, $handle = '', $is_admin = 0, $islink = 99)
	{
		if($is_admin) $this->ishtml = 1;
		if(!isset($islink)) $islink = 99;
		if($this->ishtml && ($handle == '' || $handle == 99))
		{
			if(!is_object($this->html)) $this->html = load('html.class.php','html');
			if($islink==99) $this->html->show($codexid, $this->is_update_related);
		}
		$this->search_api($codexid);
		//$this->log->set('codexid', $codexid);
		//return $this->log->add();
	}

	function search_api($codexid)
	{
		global $MODULE,$MODEL,$CATEGORY;
		if(!isset($MODULE['search'])) return false;
		if(!is_object($this->s)) $this->s = load('search.class.php', 'search', 'include');
		$r = $this->get($codexid);
		if(!$r) return false;
		//$modelid = $CATEGORY[$r['catid']]['modelid'];
		if(!$MODEL[$this->modelid]['enablesearch']) return false;
        $type = $MODEL[$this->modelid]['tablename'];
		$this->s->set_type($type);
		if($r['searchid'])
		{
			if($r['status'] == 99)
			{
				$fulltext_array = require($module.'_fields.inc.php');
				foreach($fulltext_array AS $key=>$value)
				{
					if($value['isfulltext']) $fulltextcodex .= $r[$key].' ';
				}
				$this->s->update($r['searchid'], $r['title'], $fulltextcodex, $r['url']);
			}
			else
			{
				$this->s->delete($r['searchid']);
			}
		}
		elseif($r['status']==99)
		{
			$fulltext_array = require('codex_fields.inc.php');
			foreach($fulltext_array AS $key=>$value)
			{
				//if($value['isfulltext']) $fulltextcodex .= $r[$key].' ';
			}
			$searchid = $this->s->add($r['title'], $fulltextcodex, $r['url']);
			if(!$searchid) return false;
            $this->db->query("UPDATE `$this->table` SET `searchid`=$searchid WHERE `codexid`=$codexid");
		}
		return true;
	}

    function codexid($codexid, $userid = 0, $allow_status = array())
    {
		$where = "`codexid` IN(".implodeids($codexid).")";
		$where .= $allow_status ? " AND `status` IN(".implodeids($allow_status).")" : '';
		$where .=  $this->userid_sql;
		$array = array();
		$result = $this->db->query("SELECT `codexid` FROM `$this->table` WHERE $where");
		while($r = $this->db->fetch_array($result))
		{
			$array[] = $r['codexid'];
		}
        $this->db->free_result($result);
		return is_array($codexid) ? $array : $array[0];
    }

	function delete($codexid)
	{
		global $MODULE,$attachment;
		if(is_array($codexid))
		{
			array_map(array(&$this, 'delete'), $codexid);
		}
		else
		{
			$codexid = intval($codexid);
			$data = $this->db->get_one("SELECT `catid`,`url`,`searchid` FROM `$this->table` WHERE `codexid`=$codexid $this->userid_sql");
			if($data)
			{
				$this->set_catid($data['catid']);
				if($this->ishtml) 
				{
					$html = load('html.class.php','html');
					$html->delete($codexid, $this->model_table);
				}
				
				$this->db->query("DELETE `$this->table`,`$this->model_table` FROM `$this->table`,`$this->model_table` WHERE $this->table.codexid=$this->model_table.codexid AND $this->table.codexid=$codexid $this->userid_sql");
				
                $this->db->query("UPDATE `$this->table_category` SET `items`=`items`-1 WHERE `catid`='".$data['catid']."'");
				if($this->db->affected_rows())
				{
					//$this->db->query("DELETE FROM `$this->table_count` WHERE `codexid`=$codexid");
					//$this->db->query("DELETE FROM `$this->table_position` WHERE `codexid`=$codexid");
					//$this->db->query("DELETE FROM `$this->table_tag` WHERE `codexid`=$codexid");
					
					// 删除相关附件
					if(!is_object($attachment))
					{
						require_once MOD_ROOT.'attachment/attachment.class.php';
						$attachment = new attachment($module, $data['catid']);
					}
					$attachment->delete("`codexid`=$codexid");
					
					// 删除相关评论
					/*if(isset($MODULE['comment']))
					{
						$this->db->query("DELETE FROM `".DB_PRE."comment` WHERE keyid='codex-codex-title-$codexid'");
					}*/
					
					// 删除搜索检索
					/*if(isset($MODULE['search']))
					{
						$search = load('search.class.php', 'search', 'include');
						$search->delete($data['searchid']);
					}*/
					//$this->db->query("DELETE FROM ".DB_PRE."collect WHERE `codexid`='$codexid'");
				}
				$this->log_write($codexid, 'delete');
			}
		}
		return true;
	}

	function clear()
	{
		@set_time_limit(600);
		$result = $this->db->query("SELECT `codexid` FROM `$this->table` WHERE `status`=0");
		while($r = $this->db->fetch_array($result))
		{
			$this->delete($r['codexid']);
		}
        $this->db->free_result($result);
		return true;
	}

	function restore($codexid)
	{
		return $this->status($codexid, 99);
	}

	function restoreall()
	{
		@set_time_limit(600);
		$result = $this->db->query("SELECT `codexid` FROM `$this->table` WHERE `status`=0");
		while($r = $this->db->fetch_array($result))
		{
			$this->status($r['codexid'], 99);
		}
        $this->db->free_result($result);
		return true;
	}

	function count($where = '')
	{
		if($where) $where = " WHERE $where $this->userid_sql";
		return cache_count("SELECT count(*) AS `count` FROM `$this->table` $where");
	}

	function get_count($codexid)
	{
		$codexid = intval($codexid);
		return $this->db->get_one("SELECT * FROM `$this->table` WHERE `codexid`=$codexid");
	}

	function hits($codexid)
	{
		$codexid = intval($codexid);
		$r = $this->db->get_one("SELECT * FROM `$this->table` WHERE `codexid`=$codexid");
		if(!$r) return false;
		$hits = $r['hits'] + 1;
        return $this->db->query("UPDATE `$this->table` SET `hits`=$hits WHERE `codexid`=$codexid");
	}

	// 更新文章的状态
	function status($codexid, $status, $is_admin = 0)
	{
		global $MODULE, $presentpoint,$catid;
		if(!$codexid) return false;
		$status = intval($status);
		$codexids = implodeids($codexid);
		$is_update = $this->db->query("UPDATE `$this->table` SET `status`=$status WHERE `codexid` IN($codexids) $this->userid_sql");
		if(is_array($codexid))
		{
			foreach($codexid as $id)
			{
				if($status==99 && isset($MODULE['pay']))
				{
					$cinfo = $this->get($id);
					$r = $this->db->get_one("SELECT setting FROM `".DB_PRE."category` WHERE `catid`='$cinfo[catid]'");
					$setting = string2array($r['setting']);
					$presentpoint = $setting['presentpoint'];
					$catid = $cinfo['catid'];
				}
				// 记录该文章的操作日志
				//$this->log_write($id, $status,$is_admin);
			}
		}
		else
		{
			// 记录该文章的操作日志
			//$this->log_write($codexid, $status,$is_admin);
		}
		return $is_update;
	}

	function get_codexid($title)
	{
		$info = $this->db->get_one("SELECT `codexid` FROM `$this->table` WHERE `title`='$title'");
		if($info['codexid']) return TRUE;
		else return FALSE;
	}

	function get_status($processid)
	{
		$processid = intval($processid);
		$array = array();
		$result = $this->db->query("SELECT `status` FROM `".DB_PRE."process_status` WHERE `processid`=$processid");
		while($r = $this->db->fetch_array($result))
		{
			$array[] = $r['status'];
		}
        $this->db->free_result($result);
		return $array;
	}

	function move($id = '', $targetcatid = 0, $iscatid = 0)
	{
		if($iscatid)
		{
			if(!is_array($id)) return false;
			$ids = implode(',',$id);
			$r = $this->db->select("SELECT `codexid` FROM `$this->table` WHERE `catid` IN ($ids)", 'codexid');
			$codexids = array_keys($r);
			array_map(array($this, 'move'), $codexids, array_fill(0, count($codexids), $targetcatid));
		}
		else
		{
			if(strpos($id, ',')!==false)
			{
				$codexids = explode(',', $id);
				array_map(array($this, 'move'), $codexids, array_fill(0, count($codexids), $targetcatid));
			}
			$codexid = intval($id);
			$r = $this->db->get_one("SELECT `catid`, `islink` FROM `$this->table` WHERE `codexid`=$codexid");
			$this->db->query("UPDATE `$this->table_category` SET `items`=`items`-1 WHERE `catid`=$r[catid]");
			$this->set_catid($r['catid']);
			if($this->ishtml && !$r['islink'])
			{
				if(!is_object($html))
				{
					$html = load('html.class.php','html');
				}
				$html->delete($codexid, $this->model_table);
			}
			$this->db->query("UPDATE `$this->table` SET `catid`='$targetcatid' WHERE `codexid`=$codexid");
			$info = $this->url($codexid, 0, $targetcatid);
			$this->db->query("UPDATE `$this->table` SET `url`='$info[1]' WHERE `codexid`=$codexid");
			$this->db->query("UPDATE `$this->table_category` SET `items`=`items`+1 WHERE `catid`=$targetcatid");
			$this->log_write($codexid);
		}
		return true;
	}

	function get_posid($codexid = 0, $posid = 0)
	{
		return $this->db->get_one("SELECT * FROM `".DB_PRE."codex` WHERE `codexid`=$codexid AND `posid`=$posid");
	}

	function add_posid($codexid = 0, $posid = 0)
	{
		$codexid = intval($codexid);
		$posid = intval($posid);
		if(!$codexid || !$posid) return false;
		//$this->db->query("INSERT INTO `".DB_PRE."codex` (`posid`) VALUES ('$posid')");
		$this->db->query("UPDATE `".DB_PRE."codex` SET `posid`= $posid WHERE `codexid`=$codexid");
		return true;
	}

	function add_typeid($codexid = 0, $typeid = 0)
	{
		$codexid = intval($codexid);
		$typeid = intval($typeid);
		if(!$codexid || !$typeid) return false;
		$this->db->query("UPDATE $this->table SET `typeid`=$typeid WHERE `codexid`=$codexid");
		return true;
	}

	function update_search($catid, $i)
	{
		$info = $this->db->get_one("SELECT `codexid` FROM `$this->table` WHERE `catid`='$catid' AND `status`=99 ORDER BY `codexid` DESC LIMIT $i, 1");
		$this->search_api($info['codexid']);
		return true;
	}

	function inspect($codexid, $passname = '')
	{
		global $MODEL;
		if(!$passname) return false;
		require_once MOD_ROOT.'process/process.class.php';
		if(is_array($codexid))
		{
			foreach($codexid as $c)
			{
				$r = $this->get($c);
				$this->set_catid($r['catid']);
				$workflowid = $MODEL[$this->modelid]['workflowid'];
				if(${prco.$workflowid})
				{
					$PROCESS = ${prco.$workflowid};
				}
				else
				{
					${prco.$workflowid} = $PROCESS = cache_read('process_'.$workflowid.'.php');
				}
				$PROCESS = array_keys($PROCESS);
				$re = $this->db->get_one("SELECT `processid` FROM `".DB_PRE."process` WHERE `workflowid`=$workflowid AND `passstatus`=$r[status]");
				if(!$re)
				{
					$processid = array_shift($PROCESS);
				}
				else
				{
					foreach($PROCESS AS $key => $pro)
					{
						if($pro==$re['processid'])
						{
							$key++;
							$processid = $PROCESS[$key];
							break;
						}
					}
				}
				$p = new process($workflowid);
				$allow_status = $p->get_process_status($processid);
				$codexids = $this->codexid($c, 0, $allow_status);
				$process = $p->get($processid, $passname);
				$this->status((array)$codexids, $process['passstatus']);
				unset($p);
			}
		}
		return true;
	}

	function get_pro_status()
	{
		global $priv_role;
		$status = array();
		$STATUS = $this->db->select("SELECT `status`, `name` FROM `".DB_PRE."status` WHERE `status`>2 AND `status`!=99");
		foreach($STATUS as $S)
		{
			$process = $this->db->select("SELECT `processid` FROM `".DB_PRE."process_status` WHERE `status`=$S[status]");
			foreach($process as $p)
			{
				if($priv_role->check('processid', $p['processid']))
				{
					$status[] = $S;
					break;
				}
			}
		}
		return $status;
	}
	
	// 这里设定详细页的URL规则
	/*function url($codexid, $page = 0, $catid = 0, $time = 0, $prefix = ''){
		
		global $SETTING;
		if($SETTING['ishtml']){
				
			$codexid = $prefix ? $prefix : $codexid;
			$url = $page ? "codex/{$codexid},{$page}.{$SETTING['fileext']}" : "codex/{$codexid}.{$SETTING['fileext']}";
			
		}
		else{
			// 内容页的动态地址
			$url = $page ? "./?codex/{$codexid},{$page}.{$SETTING['fileext']}" : "./?codex/{$codexid}.{$SETTING['fileext']}";
		}
		return $url;
	}*/
	
	
	function html($codexid, $is_update_related = 0)
	{
		global $MODEL,$CATEGORY;
		require_once MOD_ROOT.'codex/codex.class.php';
		$codex = new codex();
		$r = $codex->get($codexid);
		//if(!$r) return false;
		
		if($r['catid']) $catid = $r['catid'];
        //$C = cache_read('category_'.$r['catid'].'.php', '', 1);
		
		$data = $codex->output($r);
		extract($data);
		
		$head['keywords'] = str_replace(' ', ',', $r['keywords']);
		$head['description'] = $r['description'];
		
		
		$pages = $titles = '';
	
		$page = $page ? $page : 1;
		
	
		$title = strip_tags($title);
		$head['title'] = $title.'_'.$C['catname'].'_'.$SETTING['sitename'];
		$ishtml = 1;
		$url = $this->url($r['codexid'], 0, $r['catid'], $r['inputtime'],$prefix);
		
		ob_start();
		
		include template('view.html','codex');
		return createhtml($url);
	}
	
	function url($codexid, $page = 0, $catid = 0, $time = 0, $prefix = '')
	{
		global $SETTING;
		if($SETTING['ishtml']){
			$codexid = $prefix ? $prefix : $codexid;
			$url = $page ? "codex/{$codexid},{$page}.{$SETTING['fileext']}" : "codex/{$codexid}.{$SETTING['fileext']}";
		}
		else{
			// 内容页的动态地址
			//$url = $page ? "./?show/{$codexid},{$page}.{$SETTING['fileext']}" : "./?show/{$codexid}.{$SETTING['fileext']}";
			$url = $page ? "?module=codex&codexid={$codexid}&page={$page}" : "?module=codex&codexid={$codexid}";
		}
		return $url;
	}
}
?>